package com.ccc.dingtalk.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ccc.dingtalk.pojo.vo.ClockTimeVO;
import com.ccc.dingtalk.response.ResponseCode;
import com.ccc.dingtalk.response.ResponseResult;
import com.ccc.dingtalk.service.dingtalk.AttendanceService;
import com.ccc.dingtalk.service.dingtalk.ClockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 考勤
 *
 * @author superC
 * @version 1.0.0
 * @date 2023-08-25
 */

@RestController
@RequestMapping("/attendance")
public class AttendanceController {
    protected static final Logger logger = LoggerFactory.getLogger(AttendanceController.class);

    @Autowired
    private ClockService clockService;

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 获取当日考勤打卡列表
     * 【CCC20230817 弃用，直接使用全部用户查询】
     *
     * @return
     */
    @Deprecated
    @GetMapping("/getTodayClockList2")
    public ResponseResult getTodayClockList2() {
        try {
            ClockTimeVO clockTime = clockService.getClockTime();
            // 上班打卡时间
            String onTime = clockTime.getOnTime();
            /*
            判断当前时间，是否超过了上班打卡时间。
                1. 若已超过了，获取全部用户查询；
                2. 若未超，走用户分组查询
             */
            // 指定日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
            // 解析时间字符串为Date对象
            Date target = sdf.parse(DateUtil.today() + " " + onTime);
            if (DateUtil.compare(new Date(), target, DatePattern.NORM_TIME_PATTERN) > 0) {
                // 1. 已超，获取全部用户查询
                return ResponseResult.success("获取数据成功", attendanceService.getTodayAttendanceList2AllUser());
            } else {
                // 2 未超，走用户分组查询
                return ResponseResult.success("获取数据成功", attendanceService.getTodayAttendanceList2UserGroup2());
            }
        } catch (Exception e) {
            logger.error("【捕获异常-获取当日考勤打卡列表】\r\n异常记录：", e);
            return ResponseResult.failure(ResponseCode.DATA_GET_ERROR, e.getMessage());
        }
    }

}
